/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ac.manchester.cs.afzal.term_classifier.nlp;

import ac.manchester.cs.afzal.term_classifier.miscellaneous.File_Manager_Impl;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.trees.TreePrint;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;

/**
 *
 * @author Hammad
 */
public class Parser_Threaded_Impl implements Runnable
{

    String[] list_files;
    String ANNOTATED_SENTENCES_CORPUS;
    String PARSED_SENTENCES_PENN_CORPUS;
    String PARSED_SENTENCES_DEP_CORPUS;

    String directory_name;

    private File_Manager_Impl File_Manager = new File_Manager_Impl();
    private LexicalizedParser lp;
    private String STANFORD_PARSER_PATH = "D://NetBeansProjects//Resources//stanford-parser";
    private String EN_SER_MODEL = STANFORD_PARSER_PATH + "//englishPCFG.ser.gz";

    public Parser_Threaded_Impl(String[] list_files, String sentences_corpus, String directory_name, String parsed_corpus, String dep_corpus)
    {
        this.list_files = list_files;
    
        this.ANNOTATED_SENTENCES_CORPUS = sentences_corpus;
    
        this.PARSED_SENTENCES_PENN_CORPUS = parsed_corpus;
    
        this.PARSED_SENTENCES_DEP_CORPUS = dep_corpus;
    
        this.directory_name = directory_name;

        lp = new LexicalizedParser(EN_SER_MODEL);
    }

    public void run()
    {
        StringBuffer file_contents_buffer;


        for (String file_name : list_files)
        {
            File input_sentence_per_term, output_penn_per_term, output_dep_per_term;
            input_sentence_per_term = new File(ANNOTATED_SENTENCES_CORPUS + "//" + directory_name + "//" + file_name);
            input_sentence_per_term = input_sentence_per_term.getAbsoluteFile();
            file_contents_buffer = File_Manager.fileReader(input_sentence_per_term);

            output_penn_per_term = new File(PARSED_SENTENCES_PENN_CORPUS + "//" + directory_name + "//" + file_name);
            output_dep_per_term = new File(PARSED_SENTENCES_DEP_CORPUS + "//" + directory_name + "//" + file_name);
            
            System.out.println("Length: " + file_contents_buffer.length());
            System.out.println("Value: " + file_contents_buffer);
            long start = System.currentTimeMillis();

            Tree parse = null;

            try
            {
                OutputStream fout1 = new FileOutputStream(output_penn_per_term);
                PrintWriter pw1 = new PrintWriter(fout1, true);

                OutputStream fout2 = new FileOutputStream(output_dep_per_term);
                PrintWriter pw2 = new PrintWriter(fout2, true);

                parse = (Tree) lp.apply(file_contents_buffer.toString());

                TreePrint tp1 = new TreePrint("penn");
                tp1.printTree(parse, pw1);

                tp1 = new TreePrint("typedDependenciesCollapsed");
                tp1.printTree(parse, pw2);

            }
            catch (Exception ex)
            {
                System.out.println("Exception : " + ex.getMessage());
            }
            long end = System.currentTimeMillis();

            System.out.println("Execution time was " + (end - start) + " ms.");
        }
    }
}
